﻿#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"



//判断是否为有效日期
bool Date::CheakDate() const
{
	if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay())
	{
		return false;
	}
	else
	{
		return true;
	}
}

//初始化 --- 构造函数
Date::Date(int year, int month, int day)//缺省值只能在声明里面给
	:_year(year)
	, _month(month)
	, _day(day)
{
	if (!CheakDate())
	{
		cout << "非法日期: ";
		PrintDate();
		exit(1);
	}
}



bool Date::operator<(const Date& d) const
{
	if (this->_year > d._year ||
		this->_year == d._year && this->_month > d._month ||
		this->_year == d._year && this->_month == d._month && this->_day >= d._day)
	{
		return false;
	}
	else
	{
		return true;
	}

}


bool Date::operator==(const Date& d) const
{
	if (this->_year == d._year && this->_month == d._month && this->_day == d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}


bool Date::operator>(const Date& d) const
{
	return !operator<=(d);
}


bool Date::operator<=(const Date& d) const //函数复用
{
	// return *this < d || *this == d;
	//return this->operator<(d) || this->operator==(d);
	return operator<(d) || operator==(d);
}


bool Date::operator>=(const Date& d) const
{
	return !operator<(d);
}


bool Date::operator!=(const Date& d) const
{
	return !operator==(d);
}








Date& Date::operator+=(int day)
{
	//复用-=函数 防止传来一个负的天数
	if (day < 0)
	{
		return *this -= -day; //加负数 == 减正数
	}

	_day += day;
	while (_day > GetMonthDay())
	{
		_day -= GetMonthDay();
		_month++; //月进一位

		if (_month > 12)
		{
			_month -= 12;
			_year++; //年进一位
		}
	}

	return *this; //出当前局部栈帧不会销毁 可以传引用返回 减少拷贝
}


Date Date::operator+(int day) const //*this本身不变
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}



//d1 -= 天数
Date& Date::operator-=(int day)
{
	//同理
	if (day < 0)
	{
		return *this += -day; //减负数 == 加正数
	}
	_day -= day;
	while (_day < 1)
	{
		_month--;
		if (_month < 1)
		{
			_month += 12;
			_year--;
		}
		_day += GetMonthDay();//注意放在month--后面
	}

	return *this;
}


Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}


//++d1 --->d1.operator++()
//d1++ ---> d1.operator++(int)
Date& Date::operator++()
{
	return *this += 1;
}

Date Date::operator++(int)   //所以非必要时 自定义类型尽量使用前置++/-- 可以减少拷贝
{
	Date tmp = *this; //一次拷贝
	*this += 1;
	return tmp;//第二次拷贝
}


Date& Date::operator--()
{
	return *this -= 1;
}

Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

//d1 - d2 ---> d1.operator-(d2)
int Date::operator-(const Date& d) const
{
	Date before = d; //假设为小日期
	Date after = *this; //假设为大日期
	int signal = 1;

	if (*this < d)
	{
		before = *this;
		after = d;
		signal = -1;
	}

	int days = 0;
	while (before < after)
	{
		++before;
		++days;
	}

	return days * signal;
}








ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;//从左向右结合 支持连续自定义类型的流插入
}

								 //流提取值 放到d对象里面 所以不能加const
istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日-> ";

		in >> d._year >> d._month >> d._day;

		if (d.CheakDate())
		{
			break;
		}
		else
		{
			cout << "日期非法 请重新输入-> ";

		}
	}

	return in;

}








inline int fuck(int a, int b)
{
	return a + b;
}